package com.xzlf;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * java Timer 类
 */
public class TimerTest {
    public static void main(String[] args) {
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new MyTask(), 0, 2000L);
    }
}

class MyTask extends TimerTask{
    @Override
    public void run() {
        System.out.println("任务执行了：" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    }
}
